package top.liubaiblog.poh.common.util.vo;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.Data;
import top.liubaiblog.poh.common.util.vo.enums.ResponseDataEnum;

/**
 * @author 留白
 * @description
 */
@Data
public class ResponseData {

    private boolean success;    // 是否成功
    private String status;         // 状态码
    private String message;     // 响应消息
    @JsonSerialize(using = ToStringSerializer.class)
    private long timestamp;     // 响应时间
    private Object data;        // 响应数据

    /**
     * 成功的请求
     */
    public static ResponseData success(Object data) {
        ResponseData responseData = new ResponseData();
        responseData.setData(data);
        responseData.setSuccess(true);
        responseData.setMessage("请求成功");
        responseData.setStatus("00000");
        responseData.setTimestamp(System.currentTimeMillis());
        return responseData;
    }

    /**
     * 失败的请求
     */
    public static ResponseData fail(String status, String message) {
        ResponseData responseData = new ResponseData();
        responseData.setSuccess(false);
        responseData.setMessage(message);
        responseData.setStatus(status);
        responseData.setTimestamp(System.currentTimeMillis());
        return responseData;
    }

    /**
     * 根据状态码构造请求
     */
    public static ResponseData build(ResponseDataEnum responseCode) {
        ResponseData responseData = new ResponseData();
        responseData.setSuccess(ResponseDataEnum.SUCCESS.equals(responseCode));
        responseData.setMessage(responseCode.getMessage());
        responseData.setStatus(responseCode.getStatus());
        responseData.setTimestamp(System.currentTimeMillis());
        return responseData;
    }

    /**
     * 根据状态码构造请求，并含有响应结果
     */
    public static ResponseData build(ResponseDataEnum responseCode, Object data) {
        ResponseData responseData = new ResponseData();
        responseData.setSuccess(ResponseDataEnum.SUCCESS.equals(responseCode));
        responseData.setMessage(responseCode.getMessage());
        responseData.setStatus(responseCode.getStatus());
        responseData.setData(data);
        responseData.setTimestamp(System.currentTimeMillis());
        return responseData;
    }
}
